﻿package minipost.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import minipost.post.PostBean;
import minipost.post.model.Post;

public class adminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public adminServlet() {
        super();
    }

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String resultStr = null;
		request.setCharacterEncoding("UTF-8");
		try {
			String type = request.getParameter("type");
			
			if (type.equals("del")) {
				String id		= request.getParameter("id");
				PostBean postBean = new PostBean();
				postBean.deleteById(Integer.parseInt(id));
				resultStr = "1";
			} else if (type.equals("edit")) {
				String id = request.getParameter("id");
				String title = request.getParameter("title");
				String author = request.getParameter("from");
				String category = request.getParameter("category");
				String content = request.getParameter("input-content");
				Post post = new Post(title, category, author, content);
				post.setId(Integer.parseInt(id));
				PostBean postBean = new PostBean();
				postBean.update(post);
				resultStr = "修改成功";
			}
		} catch (Exception e) {
			resultStr = "未知错误";
		} finally {
		}
		response.setHeader("Content-type","text/html;charset=UTF-8");
		OutputStream stream = response.getOutputStream();
		stream.write(resultStr.getBytes("UTF-8"));
	}

}